<template>
  <div>
    <common-head title="商品列表" />
    <div class="wrap">
      <!-- 筛选 -->
      <div class="filter">
        <div
          @click="orderBySale"
          :class="[
            'order',
            'sale',
            {
              active: order.orderBy === 'sale'
            }
          ]"
        >
          <span>销量</span>
          <div
            :class="[
              'angle',
              'up',
              {
                active: order.orderBy === 'sale' && order.upDown === 'up'
              }
            ]"
          ></div>
          <div
            :class="[
              'angle',
              'down',
              {
                active: order.orderBy === 'sale' && order.upDown === 'down'
              }
            ]"
          ></div>
        </div>
        <div
          @click="orderByPrice"
          :class="[
            'order',
            'price',
            {
              active: order.orderBy === 'price'
            }
          ]"
        >
          <span>价格</span>
          <div
            :class="[
              'angle',
              'up',
              {
                active: order.orderBy === 'price' && order.upDown === 'up'
              }
            ]"
          ></div>
          <div
            :class="[
              'angle',
              'down',
              {
                active: order.orderBy === 'price' && order.upDown === 'down'
              }
            ]"
          ></div>
        </div>
      </div>
      <!-- /筛选 -->
      <!-- 商品列表 -->
      <div class="recommend">
        <div class="recommend-items">
          <div class="item" v-for="item in items" :key="item.id">
            <img class="item-pic" :src="item.pic" alt="" />
            <div class="item-info">
              <h5>{{ item.name | subStr }}</h5>
              <p>{{ item.characteristic | subStr }}</p>
              <div class="item-action">
                <p class="price">{{ item.minPrice | currency("￥") }}</p>
                <van-icon
                  class-prefix="icon"
                  name="jia-tianchong

"
                ></van-icon>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- /商品列表 -->
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { fetchItems } from '@api'
import { subStr, currency } from '@/filters'
export default {
  data () {
    return {
      order: {
        orderBy: 'sale', // price价格 sale 销量
        upDown: 'down' // up 升序 down 降序
      },
      items: [], // 商品列表
      orderBy: 'ordersDown' // 请求接口排序 参数
    }
  },
  created () {
    this.fetchItems()
  },
  methods: {
    fetchItems () {
      fetchItems({
        categoryId: this.$route.query.cate_id,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    orderBySale () {
      // 点击按照销量排序
      /*
      情况1，从按照价格排序 点击按照销量 默认按照销量降序
      情况2
        按照销量 切换 升序还是降序
      */
      if (this.order.orderBy === 'price') {
        this.order.orderBy = 'sale'
        this.order.upDown = 'down'
        this.orderBy = 'ordersDown'
      } else {
        // 按照销量 切换 升序还是降序
        if (this.order.upDown === 'up') {
          this.order.upDown = 'down'
          this.orderBy = 'ordersDown'
        } else {
          this.order.upDown = 'up'
          this.orderBy = 'ordersUp'
        }
      }

      this.fetchItems()
    },
    orderByPrice () {
      // 点击按照价格排序
      /*
      情况1，从按照销量排序 点击按照价格 默认按照销量升序
      情况2
        按照价格 切换 升序还是降序
      */
      if (this.order.orderBy === 'sale') {
        this.order.orderBy = 'price'
        this.order.upDown = 'up'
        this.orderBy = 'priceUp'
        return false
      } else {
        // 按照销量 切换 升序还是降序
        if (this.order.upDown === 'up') {
          this.order.upDown = 'down'
          this.orderBy = 'priceDown'
        } else {
          this.order.upDown = 'up'
          this.orderBy = 'priceUp'
        }
      }
      this.fetchItems()
    }
  },
  filters: {
    subStr,
    currency
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  margin-top: 46px;
  .filter {
    height: 50px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-around;
    .order {
      position: relative;
      color: #333;
      &.active {
        color: #f66;
      }
      .angle {
        width: 0;
        height: 0;
        border: 6px solid transparent;
        position: absolute;
        right: -16px;
        &.up {
          border-bottom-color: #333;
          top: -2px;
          &.active {
            border-bottom-color: #f66;
          }
        }
        &.down {
          border-top-color: #333;
          top: 12px;
          &.active {
            border-top-color: #f66;
          }
        }
      }
    }
  }
}

.recommend-items{
  .item{
    height: 180px;
    display: flex;
    background-color: #fff;
    margin-top:10px;
    .item-pic{
      width: 180px;
      height: 180px;
    }
    .item-info{
      flex:1;
      height: 180px;
      display: flex;
      flex-direction: column;
      padding:0 10px;
      h5{
        color:#333;
        font-size: 14px;
        line-height: 30px;
      }
      p{
        font-size: 12px;
        line-height: 28px;
        color:#666;
      }
      .item-action{
        height: 50px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .price{
          font-size: 16px;
          color:#f66;
        }
        .icon{
          font-size: 20px;
          color:#f66;
        }
      }
    }
  }
}

</style>
